/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-29 15:44:43
 */
/**
 * @module utils/ArrayUtil
 * @desc 数组操作工具
 */

/**
 * 数组扁平化
 * @param {Array} arr
 * @param {Array} res
 */
export function flatten(arr, res) {
  var i = 0,
    cur = null;
  var len = arr.length;
  for (; i < len; i++) {
    cur = arr[i];
    if(Array.isArray(cur)) {
      flatten(cur, res);
    } else {
      res.push(cur);
    }
  }
  return res;
}

/**
 * 获取数组最大值
 * @param {Array} arr 数组
 * @ignore
 */
function max(arr) {
  return arr.reduce((accu, curr) => {
    if (curr > accu) {
      return curr;
    }
    return accu;
  });
}
/**
 * 获取数组深度
 * @param {Array} arr 数组
 */
export function getDepth(arr) {
  let itemDepths = [];
  arr.forEach((item) => {
    let depth = 0;
    if (Array.isArray(item)) {
      depth = getDepth(item);
    }
    itemDepths.push(depth);
  });
  return 1 + max(itemDepths);
}

